#include "../include/string.h"

int atoi(char *texto)
{
    int a=0;
    int i=0;
    while (texto[i]>=0x30 && texto[i]<=0x39)
    {
        a*=10;
        a+=(texto[i]-0x30);
        i++;
    }

    return a;
}

int
strcmp(const char * s1, const char * s2)
{
    int i;

    if(s1 == NULL && s2 != NULL)
        return 1;
    else if (s1 != NULL && s2 == NULL)
        return -1;
    else if (s1 == NULL && s2 == NULL)
        return 0;
    else
    {
        for (i = 0 ; s1[i] != '\0' && s2[i] != '\0' ; i++ )
        {
            if (s1[i] < s2[i])
                return -1;
            if (s1[i] > s2[i])
                return 1;
        }

        if(s1[i] == '\0' && s2[i] != '\0')
            return -1;

        if (s2[i] == '\0' && s1[i] != '\0')
            return 1;

    }

    /* Si llega aca es porque son iguales */
    return 0;
}

int 
strncmp(const char * s1, const char * s2, unsigned int n)
{
    int i;

    if(s1 == NULL && s2 != NULL)
        return 1;
    else if (s1 != NULL && s2 == NULL)
        return -1;
    else if (s1 == NULL && s2 == NULL)
        return 0;
    else
    {
        for (i = 0 ; i < n ; i++ )
        {
            if (s1[i] < s2[i])
                return -1;
            if (s1[i] > s2[i])
                return 1;
        }
    }

    /* Si llega aca es porque son iguales */
    return 0;
}


char *
strncpy(char * dest, const char * src, unsigned int n)
{
    unsigned int i;

    for (i = 0 ; i < n && src[i] != '\0' ; i++)
        dest[i] = src[i];
    for ( ; i < n ; i++)
        dest[i] = '\0';

    return dest;
}

unsigned int 
strlen(const char * s)
{
    unsigned int i;

    for (i = 0 ; s[i] != '\0' ; i++)
    ;

    return i;
}

char *
strchr(char * s, int c)
{
    int i = 0;

    /* Recorre el arreglo, incluido su '\0' final por si el usuario invoca esta funcion con c = 0 */
    while (i < strlen(s) + 1)
    {
        if (*s == c)
            return (s);
        else if(*s == 0)
           return NULL;
        s++;
    }

    return NULL;
}

void * 
memcpy(void * dest, const void * src, unsigned int n)
{
    int i;
    unsigned char * current;

    if (dest == NULL || src == NULL || n <= 0)
        return NULL;

    for (i = 0 ; i < n ; i++)
    {
        /* Si hay memory overlap (dest interseccion src != vacio, falla la copia */
        if ( (unsigned char *)dest + i * sizeof(char) == ( (unsigned char *)src ) + i * sizeof(char) )
            return NULL;

        /* Obtiene la direccion del byte a donde tiene que escribir */
        current = (unsigned char *)dest + i * sizeof(char);

        /* escribe sobre esa posicion de memoria */
        *current = *((unsigned char *)src + i * sizeof(char));
    }

    return dest;
}

void *
memsetw(void * s, int c, unsigned int n)
{
    int i;
    unsigned short * current;

    if (s == NULL || n <= 0)
        return NULL;

    for (i = 0 ; i < n ; i++)
    {
        current = (unsigned char *)s + i * sizeof(unsigned short);
        *current = (unsigned short)c;
    }

    return s;
}

void *
memset(void * s, int c, unsigned int n)
{
    int i;
    unsigned char * current;

    if (s == NULL || n <= 0)
        return NULL;

    for (i = 0 ; i < n ; i++)
    {
        current = (unsigned char *)s + i * sizeof(unsigned char);
        *current = (unsigned char)c;
    }

    return s;
}

/*Fuente: http://en.wikibooks.org/wiki/Programming:C_strings#The_memcmp_function*/
int memcmp(const void *s1, const void *s2, size_t n)
{
    unsigned char *us1 = (unsigned char *) s1;
    unsigned char *us2 = (unsigned char *) s2;
    while (n-- != 0)
    {
        if (*us1 != *us2)
            return (*us1 < *us2) ? -1 : +1;
        us1++;
        us2++;
    }
    return 0;
}

